package com.izhang3.proxy.job;

import com.izhang3.proxy.channel.ChannelService;
import com.izhang3.proxy.check.IpCheckService;
import com.izhang3.proxy.client.CrawlClientService;
import com.izhang3.proxy.config.ChannelTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 张良
 * @desc
 * @since 2019/6/20 21:22
 */
@Component
public class ScheduledTask {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private IpCheckService ipCheckService;

    @Autowired
    private CrawlClientService crawlClientService;

    private static Logger logger = LoggerFactory.getLogger(ScheduledTask.class);

    /**
     * 打印队列中的数据
     */
    @Scheduled(cron = "0 * * * * ?")
    public void dataJob() {
        logger.info("w1:" + channelService.data(ChannelTypeEnum.CHECK));
        logger.info("w2:" + channelService.data(ChannelTypeEnum.WAITING));
    }

    /**
     * 验证刚获取的IP
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void checkWaitJob() {
        ipCheckService.check(ChannelTypeEnum.WAITING);
    }

    /**
     * 验证IP是否已过期
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void checkIpJob() {
        ipCheckService.check(ChannelTypeEnum.CHECK);
    }

    /**
     * 定时爬取IP列表
     */
    @Scheduled(cron = "30 10 * * * ?")
    public void pull() {
        crawlClientService.pull();
    }
}
